package myjava.algorithm;

public class ReverseWordsInaString {
	public static String reverseWords(String s) {
		if (s == null || s.length() == 0) {
			return "";
		}
		
		String[] arrStrings = s.split(" ");
		StringBuilder builder = new StringBuilder();
		
		for(int i = arrStrings.length - 1;i >= 0;i--)
		{
			if(!arrStrings[i].equals(""))
				builder.append(arrStrings[i]).append(" ");
		}
		
		return builder.length() == 0 ? "" : builder.substring(0, builder.length() - 1);
	}

	public static void main(String[] args) {
		//String s = "the sky is blue";
		String s = " ";
		
		System.out.println(reverseWords(s));
	}
}
